const app = getApp() //app.globalData


//index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        timeIndex: 0,
        cityList: [],
        citySelectList: [],
        saveCityList: null,
        saveCitySelectList: null,
        isShowBg: false,
        option: null,
        chartData: null,


    },
    // 显示城市列表
    showCityList(e) {
        this.setData({
            isShowBg: true,
            cityList: this.data.saveCityList ? this.data.saveCityList : this.data.cityList,
            citySelectList: this.data.saveCitySelectList ? this.data.saveCitySelectList : this.data.citySelectList,
            option:null,
        })
    },
    // 关闭城市列表
    closeCityList(e) {
        this.setData({
            isShowBg: false,
        })
    },
    // 确认选择的城市
    confirmCityList() {
        this.setData({
            isShowBg: false,
            saveCityList: this.data.cityList,
            saveCitySelectList: this.data.citySelectList,
        })
    },
    // 时间选择
    contrastTimeSelect(e) {
        let index = e.currentTarget.dataset.item
        this.setData({
            timeIndex: index
        })
    },

    // 城市选择
    selectCity(e) {
        let index = e.currentTarget.dataset.item
        let list = this.data.cityList
        let name = list[index].name
        let selectList = this.data.citySelectList
        let selectIndex = selectList.indexOf(name)


        if (selectIndex == -1) {
            if (selectList.length < 10) {
                list[index].isSelect = true
                selectList.push(name)

            } else {
                wx.showToast({
                    title: '数据不能超过10条',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
        } else {
            list[index].isSelect = false
            selectList.splice(selectIndex, 1)
        }


        this.setData({
            cityList: list,
            citySelectList: selectList
        })
    },
    // 移除城市
    removeCity(e) {
        let name = e.currentTarget.dataset.item
        let index = this.data.citySelectList.indexOf(name)
        let cityIndex = {
            currentTarget: {
                dataset: {
                    item: null
                }
            }
        }
        this.data.cityList.map((e, i) => {
            if (e.name == name) {
                cityIndex.currentTarget.dataset.item = i
            }
        })
        this.selectCity(cityIndex)
        this.setData({
            saveCityList: this.data.cityList,
            saveCitySelectList: this.data.citySelectList,
        })
    },
    // 图表
    setChart() {
        var station = [];
        let xdata = []
        let ydata = []
        let stationName = []
        for (let i in this.data.chartData) {
            station.push(this.data.chartData[i])
            stationName.push(i)
        }
        station.forEach(list => {
            let yd = []
            list.map(e => {
                if (xdata.length < list.length) {
                    xdata.push(e.querytime)
                }
                yd.push(e.pm25onehour)
            })
            ydata.push(yd);
        })
        var ses = [];
        var colorList = [
            ['rgba(84,112,198,0.5)', 'rgba(84,112,198,0)'],
            ['rgba(145,204,117,0.5)', 'rgba(145,204,117,0)'],
            ['rgba(250,200,88,0.5)', 'rgba(250,200,88,0)'],
            ['rgba(238,102,102,0.5)', 'rgba(238,102,102,0)'],
            ['rgba(115,192,222,0.5)', 'rgba(115,192,222,0)'],
            ['rgba(59,162,114,0.5)', 'rgba(59,162,114,0)'],
            ['rgba(252,132,82,0.5)', 'rgba(252,132,82,0)'],
            ['rgba(154,96,180,0.5)', 'rgba(154,96,180,0)'],
            ['rgba(234,124,204,0.5)', 'rgba(234,124,204,0)'],
            ['rgba(84,112,198,0.5)', 'rgba(84,112,198,0)'],
        ];
        for (let i = 0; i < station.length; i++) {
            ses.push({
                name: station[i][0].stationname,
                type: 'line',
                stack: 'Total',
                data: ydata[i],
                showSymbol: false,
                smooth: true,
                sampling: 'average',
                areaStyle: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                        offset: 0,
                        color: colorList[i][0] // 0% 处的颜色
                    }, {
                        offset: 1,
                        color: colorList[i][1] // 100% 处的颜色
                    }],
                    global: false // 缺省为 false
                },
                axisLabel: {
                    //show: true,
                    textStyle: {
                        color: '#06A9C9'
                    },

                }
            });
        }
        this.setData({
            option: {
                grid: {
                    bottom: '40%',
                },
                xAxis: {
                    //name: '时间',
                    axisLine: {
                        show: false
                    },
                    name: '',
                    type: 'category',
                    boundaryGap: false,
                    data: xdata,
                    axisLabel: {
                        formatter: function (params) {
                            let newParamsName = [
                                '{a|' + params.split(' ')[0] + '}',
                                '{b|' + params.split(' ')[1] + '}'
                            ].join('\n')
                            return newParamsName
                        },
                        align: 'left',
                        rich: {
                            a: {
                                color: '#333',
                                fontSize: 12,
                                fontWeight: 600,
                            },
                            b: {
                                color: '#2575FF',
                                fontSize: 11,
                                fontWeight: 600,
                            },
                        },
                        // interval:0,
                        rotate: 0, //倾斜度 -90 至 90 默认为0
                        // margin:2,
                        textStyle: {
                            //fontWeight:"bolder",
                            color: '#06A9C9',
                        }
                    },
                },
                yAxis: {
                    //show:false,
                    type: 'value',
                    //name: 'PM2.5'
                    axisLine: {
                        show: false, //y轴线消失
                    },
                    axisLabel: {
                        //show: true,
                        textStyle: {
                            color: '#777777',
                            fontSize: '12',
                            fontWeight: 600,
                        }
                    },
                    axisTick: {
                        show: false //y轴坐标点消失
                    },
                },
                legend: {
                    show: true,
                    top: '70%',
                    left: 0,
                    width: '100%',
                    align: 'left',
                },
                series: ses
            }
        })

    },
    //   获取城市列表
    getCityList(url) {
        app.globalData.http.get(url).then(res => {
            res.data.items.map(e => {
                if (e.position_name) {
                    e.stationname = e.position_name
                } else {
                    e.name = e.stationname
                }
                e.isSelect = false
            })
            this.setData({
                cityList:res.data.items
            })
        })
    },
    // 测站对比
    getStationMsg() {
        let list = this.data.citySelectList
        let time = this.data.timeIndex == 0 ? '24小时' : this.data.timeIndex == 1 ? '48小时' : '15天'
        app.globalData.http.get('/ai-sensing-back-service/api/publishData/compare', {
            type: time,
            stations: JSON.stringify(list).replace("[", "").replace("]", "")
        }).then(res => {
            this.setData({
                chartData:res.data.items
            })
            this.setChart()
        })


    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '测站对比',
        })
        this.getCityList("/ai-sensing-back-service/api/publishData/lastdata")

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})